package com.lhkj.ct.meta.modules.questionnaire.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.meta.modules.psychology.model.vo.ViewAnswerVo;
import com.lhkj.ct.meta.modules.questionnaire.model.entity.TblQnrAllotRecord;
import com.lhkj.ct.meta.modules.questionnaire.model.vo.QnrStatisticsVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * <p>
 * 问卷分配记录 Mapper 接口
 * </p>
 *
 * @author jobob
 * @since 2023-10-26
 */
public interface QnrAllotRecordMapper extends BaseMapper<TblQnrAllotRecord> {

    /**
     * 问卷分配记录
     */
    IPage<TblQnrAllotRecord> selectAllotPage(Page<TblQnrAllotRecord> page, @Param("record")TblQnrAllotRecord record,@Param("pageQuery") PageQuery pageQuery);

    /**
     * 查询分配记录（包含档案信息，报告id）
     * @param allotId
     * @return
     */
    TblQnrAllotRecord selectRecordWithRid(@Param("allotId") String allotId);

    /**
     * 调查分析
     */
    IPage<QnrStatisticsVo> statistics(Page<TblQnrAllotRecord> page, @Param("qnrName") String qnrName);

    /**
     * 查看作答结果
     */
    ViewAnswerVo viewAnswer(@Param("allotId") String allotId);

    /**
     * 统计用户未完成问卷
     */
    @Select("select count(t.id) from tbl_qnr_allot_record t left join tbl_qnr_result t1 on t1.allot_id = t.id " +
            "where t.pat_id = #{patId,jdbcType=VARCHAR} and t.del_flag = ${@com.lhkj.ct.base.enums.DeleteStatus@NORMAL.code} and (t1.status is null or t1.status = 0)")
    Long countNotDoneByPatient(@Param("patId") String patId);
}
